package com.example.sql_damo;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class register extends Activity {
  private DBhelpen dBhelpen;
  private SQLiteDatabase sqLiteDatabase;
    private EditText name;
    private EditText password;
    private EditText email;
    private Button registers;
    private String names;
    private String passwords;
    private Button returnss;
    private EditText emails;
    private String emailtext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        name = findViewById(R.id.zc_name);
        password = findViewById(R.id.zc_password);
        emails =findViewById(R.id.zc_email);

        returnss =findViewById(R.id.zc_return);
        registers = findViewById(R.id.zc_register);
        dBhelpen=new DBhelpen(this);
        returnss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(register.this,MainActivity.class);
                startActivity(intent);
            }
        });
        registers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                names = name.getText().toString();
                passwords = password.getText().toString();
                emailtext =emails.getText().toString();
                if (judge()){
                    init();
                }

            }
        });
    }

    private boolean judge() {
        if (TextUtils.isEmpty(names)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (passwords.length() < 8) {
            Toast.makeText(this, "密码名不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (null==emailtext || "".equals(emailtext)) {

            Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
            //Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
            //Matcher m = p.matcher(emailtext);
            Toast.makeText(this, "邮箱名不能为空", Toast.LENGTH_SHORT).show();
            return false;


    }
       return  true;}



    private void init() {
        names = name.getText().toString();
        passwords = password.getText().toString();
        emailtext =emails.getText().toString();

        sqLiteDatabase=dBhelpen.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("NAME", names);
        contentValues.put("PAS", passwords);
        contentValues.put("EMAIL",emailtext);

        sqLiteDatabase.insert("INFOMATION",null,contentValues);
        sqLiteDatabase.close();
        Toast.makeText(register.this, "成功", Toast.LENGTH_SHORT).show();
        inten();
    }

     public void inten(){
         Intent intent=new Intent(register.this,MainActivity.class);
         startActivity(intent);
     }
}
